﻿using System.Linq;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using System.Windows;
using System.ComponentModel;

namespace OpenLightGroup.Common.Interactivity
{
    /// <summary>
    /// Toggles the visibility of a UIElement based on the current user's roles.
    /// </summary>
    [Description("Toggles the visibility of the UIElement based on the current user's roles.")]
    public class RoleBasedVisibilityBehavior
        : BaseRoleBasedBehavior<UIElement>
    {

        /// <summary>
        /// Updates the associated UIElement
        /// </summary>
        /// <param name="sender">Not used.</param>
        /// <param name="e">Not used.</param>
        protected override void UpdateAssociatedObject(object sender, AuthenticationEventArgs e)
        {
            if (this.AllowedRoles.Where(r => IsInRole(r)).Count() > 0)
            {
                this.AssociatedObject.Visibility = Visibility.Visible;
            }
            else
	        {
                this.AssociatedObject.Visibility = Visibility.Collapsed;
	        }
        }
    }
}
